package com.lixixi.tingshu.album.api;

import com.lixixi.tingshu.album.service.FileUploadService;
import com.lixixi.tingshu.common.anno.NeedLogin;
import com.lixixi.tingshu.common.result.Result;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("/api/album")
public class FileUploadApiController {
	@Resource
	FileUploadService fileUploadService;

	@PostMapping(value = "/fileUpload", consumes = "multipart/form-data")
	@NeedLogin
	Result<String> fileUpload(@RequestPart MultipartFile file) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
		String filePath = fileUploadService.fileUpload(file.getInputStream(), file.getOriginalFilename(), file.getSize(), file.getContentType());
		return Result.ok(filePath);
	}
}
